package com.hbsx.why.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.hbsx.why.R;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liub
 * time 2017/8/29
 */

public abstract class PopMenu<T> {
    private List<T> mitemList;
    private Context context;
    private PopupWindow popupWindow;
    private RecyclerView recyclerView;

    // private OnItemClickListener listener;
    public PopMenu(Context context, List<T> itemList) {
        this.context = context;
        mitemList = new ArrayList<T>();
        mitemList = itemList;
        View view = LayoutInflater.from(context).inflate(getLayoutManu(), null);
        // 设置 recyclerView
        recyclerView = (RecyclerView) view.findViewById(R.id.popwin_listView);
        setLinearLayout(recyclerView, context);
        recyclerView.setAdapter(new PopAdapter(itemList));
        recyclerView.setFocusableInTouchMode(true);
        recyclerView.setFocusable(true);
        // popupWindow = new PopupWindow(view, 100, LayoutParams.WRAP_CONTENT);
        popupWindow = new PopupWindow(view, RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);
        // context.getResources()
        // .getDimensionPixelSize(R.dimen.popmenu_width)
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    // 批量添加菜单项
    public void addItems(List<T> items) {
        for (T s : items)
            mitemList.add(s);
    }

    // 单个添加菜单项
    public void addItem(T item) {
        mitemList.add(item);
    }

    // 下拉式 弹出 pop菜单 parent 右下角
    public void showAsDropDown(View parent) {
        popupWindow.showAsDropDown(parent, 10,
                // 保证尺寸是根据屏幕像素密度来的
                context.getResources().getDimensionPixelSize(R.dimen.size10dp));
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // // 刷新状态
        popupWindow.update();
    }

    // 隐藏菜单
    public void dismiss() {
        popupWindow.dismiss();
    }

    public abstract int getLayoutManu();

    // 适配器
    private final class PopAdapter extends LGRecycleViewAdapter<T> {

        public PopAdapter(List<T> dataList) {
            super(dataList);
        }

        @Override
        public int getLayoutId(int viewType) {
            return getmLayoutId();
        }

        @Override
        public void convert(LGViewHolder holder, T t, int position) {
            binderconvert(holder, t, position);
        }
    }

    protected abstract int getmLayoutId();

    protected abstract void binderconvert(LGViewHolder holder, T t, int position);

    /**
     * 线性布局--可滑动
     */
    public static void setLinearLayout(RecyclerView pullRecycleView, Context mContext) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(linearLayoutManager);
    }
}
